package com.hz.ruoyilgorder.rabbitmq;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hz.rouyigonggong.domain.LgSellerOrder;
import com.hz.ruoyilgorder.service.LgSellerOrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @Program: deadletterRabbitMQ
 * @Description:  死信队列消费者
 * @Author: jone
 * @Create: 2025-02-13 14:23
 **/
@Component
public class DeadLetterConsumer {
    @Autowired
    LgSellerOrderService lgSellerOrderService;

    @RabbitListener(queues = RabbitMQConfig.DELAY_QUEUE)
    @RabbitHandler
        public void deadLetterListener(Message message, Channel channel){
        System.out.println("进入死信消费者。。。。。");
        Integer orderNumber = Integer.parseInt(new String(message.getBody()));
        System.out.println("死信队列消息："+orderNumber);
        QueryWrapper<LgSellerOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_number",orderNumber);
        LgSellerOrder lgSellerOrder = lgSellerOrderService.getOne(queryWrapper);
        if (lgSellerOrder.getDepositPaid() == 2){
            lgSellerOrderService.removeById(lgSellerOrder.getId());
        }
        try {
            //手动回馈 false 单条消息应答 当为 true 时批量应答
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
        } catch (Exception e) {
                // 处理条数 是否批量处理 是否放回队列 false丢弃
            try {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(), false,true);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    }


}
